package com.medical.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.HttpRequest;
import com.medical.po.HospitalberthCustom;
import com.medical.po.Hospitaldept;
import com.medical.po.HospitaldeptCustom;
import com.medical.service.HospitalBerthService;
import com.medical.service.HospitaldeptService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年8月15日 上午10:00:45 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/Berth")
public class BerthController {
	/**
	 * 注入HospitaldeptService服务
	 */
	@Autowired
	private HospitaldeptService hospitaldeptService;
	@Autowired
	private HospitalBerthService hospitalBerthService;
	/**
	 * 初始化床位设置的界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/berthset")
	public ModelAndView berthset()throws Exception {
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/Hospital/BerthSet");
		return model;
	}
	/**
	 * 获取各个科室的详情
	 * @param hospitaldeptCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/gethospitaldept")
	public @ResponseBody Hospitaldept gethospitaldept(@RequestBody HospitaldeptCustom hospitaldeptCustom)throws Exception {
		System.out.println("输出接收的参数是："+hospitaldeptCustom.getHospdeptid());
		Integer hospdeptid = hospitaldeptCustom.getHospdeptid();
		Hospitaldept Hospitaldept = hospitaldeptService.findHospitalDeptAbs(hospdeptid);
		return Hospitaldept;
	}
	/**
	 * 医院部门树初始化
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/myhospDeptInit")
	public @ResponseBody Map<String,Object> myhospDeptInit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid= (Integer)session.getAttribute("hosploginid");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> datalist=new ArrayList<>();
		String param = "hosploginid="+hosploginid;
		try {
			JSONObject json = HttpRequest.sendPost("myhospdeptinit", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					if(json.get("data").toString()!="null"){
						JSONArray data = json.getJSONArray("data");
						for(int i=0;i<data.length();i++) {
							Map<String,Object> map = new HashMap<String,Object>();
							JSONObject job = data.getJSONObject(i);
							map.put("hospdeptid", job.get("hospdeptid").toString());
							map.put("hospdeptfatherid", job.get("hospdeptfatherid").toString());
							map.put("hospdeptname", job.get("hospdeptname").toString());
							map.put("total", job.get("total").toString());
							datalist.add(map);
						}
						result.put("list", datalist);
					}else {
						result.put("list", "");
					}
				} catch (Exception e) {
					System.out.println("出错了，错误信息是："+e.getMessage());
					result.put("list", "");
				}
			}else {
				result.put("list", "");
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误！");
			result.put("list", "");
		}
		return result;
	}
	
	/**
	 * 获取表hospitalberth的信息，即根据hospid和hospberthdeptid获取信息
	 * @param hospitalberthCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/gethospitalberth")
	public @ResponseBody Map<String,Object> gethospitalberth(HttpServletRequest request) throws Exception {
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		Integer hospberthdeptid = Integer.parseInt(request.getParameter("hospberthdeptid"));
		String param = "hosploginid="+hosploginid+"&hospberthdeptid="+hospberthdeptid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject datajson = HttpRequest.sendPost("gethospitalberth", param);
			String jsoncode = datajson.get("code").toString();
			String jsonmsg = datajson.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					if(datajson.get("data").toString()!="null"){
						JSONArray data = datajson.getJSONArray("data");
						for(int i=0;i<data.length();i++) {
							Map<String,Object> map = new HashMap<String,Object>();
							JSONObject job = data.getJSONObject(i);
							map.put("hospberthid", job.get("hospberthid").toString());
							map.put("hospdeptname", job.get("hospdeptname").toString());
							map.put("hospdeptabs", job.get("hospdeptabs").toString());
							map.put("hospberthname", job.get("hospberthname").toString());
							map.put("hospberthabs", job.get("hospberthabs").toString());
							list.add(map);
						}
						result.put("list", list);
					}else {
						result.put("list", "");
					}
				} catch (Exception e) {
					System.out.println("出错了，错误信息是："+e.getMessage());
					result.put("list", "");
				}
			}else {
				result.put("list", "");
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误！");
			result.put("list", "");
		}
		//hospitalberthCustom.setHospid(hospid);
		//List<HospitalberthCustom> hospitalberthList = hospitalBerthService.findhospitalberthList(hospitalberthCustom);
		//return hospitalberthList;
		return result;
	}
	/**
	 * 该方法是实现动态为各个部门添加床位，前台实现循环，后台实现一对一添加，自定义的SQL操作，返回添加数据的主键即hospberthid
	 * @param hospitalberthCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addberthSubmit")
	public @ResponseBody Map<String,Object> addberthSubmit(HttpServletRequest request)throws Exception {
		/*System.out.println("输出接收到的数据是："+hospitalberthCustom.getHospberthdeptid());
		System.out.println("输出接收到的数据是："+hospitalberthCustom.getHospberthname());
		System.out.println("输出接收到的数据是："+hospitalberthCustom.getHospberthabs());
		System.out.println("输出接收到的数据是："+hospitalberthCustom.getHospid());
		try {
			int result = hospitalBerthService.insertSelectiveReturnID(hospitalberthCustom);
			System.out.println("输出返回的ID是："+hospitalberthCustom.getHospberthid());
			if(result!=0) {
				return hospitalberthCustom.getHospberthid();
			}else {
				return -1;
			}
		} catch (Exception e) {
			return -1;
		}*/
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		Integer hospberthdeptid=Integer.parseInt(request.getParameter("hospberthdeptid"));
		String hospberthname=request.getParameter("hospberthname");
		String hospberthabs=request.getParameter("hospberthabs");
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		String param = "hosploginid="+hosploginid+"&hospberthdeptid="+hospberthdeptid+"&hospberthname="+hospberthname+"&hospberthabs="+hospberthabs;
		try {
			JSONObject datajson = HttpRequest.sendPost("addhospberth", param);
			String jsoncode = datajson.get("code").toString();
			String jsonmsg = datajson.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					JSONObject data = datajson.getJSONObject("data");
					Integer hospberthid = data.getInt("hospberthid");
					result.put("hospberthid", hospberthid);
				} catch (Exception e) {
					System.out.println("出错了，错误信息是："+e.getMessage());
					result.put("hospberthid", null);
				}
			}else {
				result.put("hospberthid", null);
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误！");
			result.put("hospberthid", null);
		}
		return result;
	}
	/**
	 * 删除床位类，根据传给的id删除床位
	 * @param hospberthid
	 * @return
	 */
	@RequestMapping(value="/deleteberthSubmit")
	public @ResponseBody Map<String,Object> deleteberthSubmit(HttpServletRequest request)throws Exception{
		/*System.out.println("输出接收到的hospberthid是:"+hospberthid);
		try {
			int result = hospitalBerthService.deleteHospitalberth(hospberthid);
			return result;
		} catch (Exception e) {
			return -1;
		}*/
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		Integer hospberthid = Integer.parseInt(request.getParameter("hospberthid"));
		String param ="hosploginid="+hosploginid+"&hospberthid="+hospberthid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			JSONObject datajson = HttpRequest.sendPost("deletehospberth", param);
			String jsoncode = datajson.get("code").toString();
			String jsonmsg = datajson.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误！");
		}
		return result;
	}
	@RequestMapping(value="/MultideleteberthSubmit")
	public @ResponseBody Map<String,Object> MultideleteberthSubmit(HttpServletRequest request)throws Exception{
		/*System.out.println("输出接收到的hospberthid："+hospberthid);
		JSONObject jsonObj = new JSONObject(hospberthid);
		System.out.println("取出json的数据的value值："+jsonObj.getString("hospberthids"));
		String berthid = jsonObj.getString("hospberthids");
		String[] berthids = berthid.split(",");
		int result=0;
		for (String ids : berthids) {
			Integer id = Integer.parseInt(ids);//强制转换String类型为Integer类型，因为表doctorlogininfo的docloginid为integer类型，需要保持一致
			System.out.println("循环输出这个ID————》"+id);
			try {
				result = hospitalBerthService.deleteHospitalberth(id);
				if(result!=1) {
					return id;
				}
			}catch (Exception e) {
				return -1;
			}
		}
		return result;*/
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		String hospberthids = request.getParameter("hospberthids");
		
		Map<String,Object> result = new HashMap<String,Object>();
		String[] berthids = hospberthids.split(",");
		try {
			for(String ids:berthids) {
				Integer hospberthid=Integer.parseInt(ids);
				String param ="hosploginid="+hosploginid+"&hospberthid="+hospberthid;
				try {
					JSONObject datajson = HttpRequest.sendPost("deletehospberth", param);
					String jsoncode = datajson.get("code").toString();
					String jsonmsg = datajson.get("msg").toString();
					
					result.put("code", jsoncode);
					result.put("msg", jsonmsg);
					if("100".equals(jsoncode)) {
						result.put("hospberthid", "");
					}else {
						result.put("hospberthid", hospberthid);
					}
				} catch (Exception e) {
					result.put("code", -100);
					result.put("msg", "数据传输错误！");
					result.put("hospberthid", "");
				}
			}
			return result;
		} catch (Exception e) {
			result.put("code", -200);
			result.put("msg", "数据解析错误！");
			result.put("hospberthid", "");
			return result;
		}
	}
}
